package com.applet.applet_shangcheng.service;

import com.applet.applet_shangcheng.dao.AddressMapper;
import com.applet.applet_shangcheng.dao.OrderMapper;
import com.applet.applet_shangcheng.dao.UserMapper;
import com.applet.applet_shangcheng.entity.*;
import com.applet.applet_shangcheng.utlis.IdWorker;
import com.applet.applet_shangcheng.utlis.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private AddressMapper addressMapper;

    public List<Address> findByAddress(String username) {
        List<Address> addressList = addressMapper.findByAddress(username);
        return addressList;
    }



    /*
     code:推荐码
     consignee:收件人
     consigneeAddress:收货地址
     phone:收件人手机号
     userId:用户id
     goodsId:商品id
     recommended:推荐人id
     number:商品个数
     */
    @Transactional(rollbackFor = Exception.class)
    public Order insertOrder(String code, String consignee, String consigneeAddress, String phone, String userId, int goodsId, int number) {
        Order order = new Order();
        long nextId = idWorker.nextId();
        order.setOrderId(String.valueOf(nextId));
        Goods goods = goodsService.findGoods(goodsId);
        //int i = goodsService.updateNumber(goodsId, number);
        if (goods == null) {
            return null;
        }
        order.setCode(code);
        order.setConsigneeAddress(consigneeAddress);
        order.setConsignee(consignee);
        order.setPhone(Integer.parseInt(phone));
        order.setGoodsName(goods.getGoodsName());
        order.setSumNumber(number);
        int sumPrice = number * goods.getPrice();
        order.setSumPrice(sumPrice);
        int remit = goods.getRemit();
        order.setRemitMoney(remit);
        int actualPay = sumPrice - remit;
        order.setActualPay(actualPay);
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        User user = userMapper.getUser(userId);
        order.setUsername(user.getUsername());
        order.setOrderStatus(0);//订单状态,0:未完成,1:已完成，2：已退货
        order.setPayStatus(0);//支付状态,0:未支付，1：已支付，2：支付失败
        order.setEmissionStatus(0);//发货状态,0:未发货，1：已发货，2：已收货
        order.setPayMode(0);
        int insertOrder = orderMapper.insertOrder(order);
        if (insertOrder > 0) {
            return order;
        }
        return null;
    }

    public List<Order> getOrder() {
        return orderMapper.getOrderList();
    }

    public PageResult findAll(String queryString, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Order> page = orderMapper.findAll(queryString);
        //PageInfo<Announcement> pageInfo = new PageInfo<>(announcementList);
        return new PageResult(page.getTotal(),page.getResult() );
    }

    public Order getOrderById(int id) {
        return orderMapper.getOrderById(id);
    }

    public int updateOrder(Order order) {
        return orderMapper.updateOrder(order);
    }

    public Order findByOrderId(String order_id) {
        return orderMapper.findByOrderId(order_id);
    }

    public void save(Order order) {
        orderMapper.save(order);
    }

    public void saveIntegral(String code) {

    }

    public List<String> getHasPay() {
       List<String> list =orderMapper.getOrderClass();
        return list;
    }

    public int getHasPay(String month) {
       int hasPay = orderMapper.getHasPay(month);
        return hasPay;
    }

    public int getNotPay(String month) {
        int NotPay = orderMapper.getNotPay(month);
        return NotPay;
    }

    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        String time = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        System.out.println(time);
    }

    public List<Map<String, Object>>getGoodsNameList(String time) {
        return orderMapper.getHasGoodsNameList(time);
    }

    public List<Map<String, Object>>getHasIGoodsNameList(String time) {
        return orderMapper.getHasIGoodsNameList(time);
    }

    public List<Map<String, Object>> getGoodsNameLists(String datetime, String datetime2) {
        return null;
    }

}
//订单状态,0:未完成,1:已完成，2：已退货
//支付状态,0:未支付，1：已支付，2：支付失败
//发货状态,0:未发货，1：已发货，2：已收货



